<template>
  <div class="add-role-container">
    <el-form ref="roleRef" :model="roleForm" :rules="roleRule" label-position="left" label-width="80px">
      <el-form-item label="角色名" prop="rolename">
        <el-input v-model="roleForm.rolename" autocomplete="off" placeholder="请输入角色名" />
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="roleForm.nickname" autocomplete="off" placeholder="请输入昵称" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="roleForm.description" autocomplete="off" placeholder="请对这个角色做一些描述" type="textarea" :rows="5" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="resetForm('roleRef')">重 置</el-button>
      <el-button type="primary" @click="saveRole">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { addRole } from '@/api/role'
export default {
  name: 'AddRole',
  components: {},
  props: {},
  data () {
    return {
      roleForm: {
        rolename: '',
        nickname: '',
        description: ''
      },
      roleRule: {
        rolename: [
          { required: true, message: '请输入角色名', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        nickname: [
          { required: true, message: '请输入角色昵称', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    saveRole: async function () {
      console.log(this.roleForm)
      const res = await addRole(this.roleForm)
      if (res) {
        this.$message({ message: '添加成功', type: 'success', duration: 1000 })
        this.$emit('reset-roles')
      }
    },
    resetForm(refName) {
      this.$refs[refName].resetFields()
    }
  }
}
</script>

<style scoped lang="scss"></style>
